# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from schoolInfo.settings import mongo_host,mongo_port,mongo_name,mongo_db_collection,mongo_user,mongo_pwd
import pymongo
import logging

class SchoolinfoPipeline:
    def __init__(self):
        host = mongo_host
        port = mongo_port
        dbname = mongo_name
        user = mongo_user
        pwd = mongo_pwd
        sheetname = mongo_db_collection
        # url = ('mongodb://',user,':',pwd,'@',host,':',port,'/',dbname)
        url = f'mongodb://{user}:{pwd}@{host}:{port}/{dbname}'
        print('mongourl:',url)
        client = pymongo.MongoClient(url)
        mydb = client[dbname]
        self.post = mydb[sheetname]

    def process_item(self, item, spider):
        data = dict(item)
        print("插入/更新数据",data)
        self.post.insert(data)
        return item
